﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class AdminPermissionsService : IAdminPermissionsService
    {
        private IAdminPermissionsManager _iam;
        public AdminPermissionsService(IAdminPermissionsManager iam)
        {
            _iam = iam;
        }
        public async Task<int> AddUsersAsync(int rolesid, int menuId)
        {
            return await _iam.AddAsync(new Model.AdminPermissions 
            {
                RolesId = rolesid,
                MunuId = menuId,
            });
        }

        public async Task<int> DeleteUsersAsync(int id)
        {
            var data = await _iam.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _iam.DeleteAsync(data);
        }

        public async Task<List<AdminPermissionsDto>> GetAllUsersAsync(int deleteId = 1)
        {
            var data = await _iam.GetChooseAsync(ap => ap.DeleteId == deleteId);
            return data == null ? new List<AdminPermissionsDto>() : data.Select(ap => new AdminPermissionsDto 
            {
                Id = ap.Id,
                RolesId = ap.RolesId,
                MunuId = ap.MunuId,
                CreateTime = ap.CreateTime,
                UpdateTime = ap.UpdateTime
            }).ToList();
        }

        public async Task<AdminPermissionsDto> GetByIdAsync(int id)
        {
            var data = await _iam.GetById(id);
            return data == null ? new AdminPermissionsDto() : new AdminPermissionsDto 
            {
                Id = data.Id,
                RolesId = data.RolesId,
                MunuId = data.MunuId,
                CreateTime = data.CreateTime,
                UpdateTime = data.UpdateTime
            };
        }

        public async Task<AdminPermissionsDto> GetDataByMenuIdAndRolesIdAsync(int sysmenuId, int rolesId, int deleteId = 1)
        {
            var data = await _iam.GetChooseAsync(am => am.MunuId == sysmenuId && am.RolesId == rolesId && am.DeleteId == deleteId);
            return data.Select(p => new AdminPermissionsDto 
            {
                Id = p.Id,
                RolesId = p.RolesId,
                DeleteId = p.DeleteId,
                MunuId = p.MunuId,
                UpdateTime = p.UpdateTime
            }).First();
        }

        public async Task<List<AdminPermissionsDto>> GetDateByRolesId(int rolesId, int deleteId = 1)
        {
            var data = await _iam.GetChooseAsync( am => am.RolesId == rolesId && am.DeleteId == deleteId);
            return data == null ? new List<AdminPermissionsDto>() : data.Select(p => new AdminPermissionsDto
            {
                Id = p.Id,
                RolesId = p.RolesId,
                DeleteId = p.DeleteId,
                MunuId = p.MunuId,
                UpdateTime = p.UpdateTime
            }).ToList();
        }

        public async Task<List<int>> GetSystemMenuIdByRolesId(int rolesId, int deleteId = 1)
        {
            var data = await _iam.GetChooseAsync(am => am.RolesId == rolesId && am.DeleteId == deleteId);

            if (data == null)
                return null;
            var list = new List<int>();
            foreach(var i in data)
                list.Add(i.MunuId);
            return list;
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _iam.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _iam.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _iam.GetById(id);
            if(data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _iam.UpdateAsync(data);
        }

        public async Task<int> UpdateUsersAsync(int id, int rolesid, int menuId)
        {
            var data = await _iam.GetById(id);
            if (data == null)
                return -1;
            data.RolesId = rolesid;
            data.MunuId = menuId;
            data.UpdateTime = DateTime.Now;
            return await _iam.UpdateAsync(data);
        }
    }
}
